'use strict';

var Model = require('../model/shop.js');
var debug = require('debug')('shop');

function Dao() {}
var selector = {
    __v: 0
}

Dao.prototype.save = function*(json) {
    console.log(json);
    console.log('-----------------');
    var entity = new Model({
        vendor_id: json.vendorId,
        name: json.name,
        address: json.address,
        telephone: json.telephone,
        description: json.description,
        images_url: json.images_url || [],
        web_url: json.web_url || [],
        district_id: json.district_id,
        loc: {
            type: "Point",
            coordinates: json.loc || [0, 0]
        },
        cate_id: json.cate_id
    });

    console.log(entity);

    var result = yield entity.save();
    return result;
}

// 查询
Dao.prototype.findByVendorId = function*(vendorId) {
    debug('findByVendorId', vendorId);
    var result = yield Model.find({
        vendor_id: vendorId
    }, selector)
    .populate('district_id')
    .populate('cate_id');

    console.log(result);
    return result;
}

module.exports = new Dao();